﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;
using PSDLib.Ctrl;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDManager.SystemSettings;

namespace PSDManager
{
    public partial class PSDClientDevice : UserControl
    {
        public ManagerHelper ManagerHelper { get; set; }

        private PSDState psdState;
        public PSDClientDevice()
        {
            InitializeComponent();

            //this.palDisk.Controls[0].Visible = false;
            //this.palDisk.Controls[1].Visible = false;
            this.palDeviceName.Visible = false;
            this.palProcess.Visible = false;

            this.toolTip1.SetToolTip(picEditDeviceName, "设置设备名称");
            this.toolTip1.SetToolTip(picEditProcess, "设置设备下载进程数");
            this.toolTip1.SetToolTip(picEditReDL, "重新启动下载服务");
            this.toolTip1.SetToolTip(picEditReboot, "重新启动设备");
            this.toolTip1.SetToolTip(picEditShutdown, "关闭设备");
        }

        public void SetDevice(PSDState ps)
        {
            psdState = ps;

            try
            {
                palDeviceName.Visible = false;
                palProcess.Visible = false;

                if (psdState != null)
                {
                    this.lblDeviceName.Text = psdState.DeviceInfo.device_name;
                    this.lblDeviceIP.Text = psdState.DeviceInfo.device_ip + ":" + psdState.DeviceInfo.device_port.ToString();
                    this.lblVersion.Text = psdState.DeviceInfo.device_version;
                    this.txtProcess.Value = psdState.DeviceInfo.max_download_process;

                    if (psdState.Online)
                    {
                        this.lblState.ForeColor = Color.Green;
                        this.lblState.Text = "在线";
                    }
                    else
                    {
                        this.lblState.ForeColor = Color.Gray;
                        this.lblState.Text = "离线";
                    }

                    //palDisk.Controls.Clear();
                    for (int i = palDisk.Controls.Count - 1; i >= 0; i--)
                    {
                        int index = palDisk.Controls.Count - 1 - i;

                        if (index < psdState.DeviceInfo.StorageList.Count)
                        {
                            SetStorage(palDisk.Controls[i] as DiskInfo, psdState.DeviceInfo.StorageList[index]);
                            palDisk.Controls[i].Visible = true;
                        }
                        else
                        {
                            palDisk.Controls[i].Visible = false;
                        }
                    }

                    //DiskInfo disk_main = palDisk.Controls[1] as DiskInfo;
                    //if (!string.IsNullOrEmpty(psdState.DeviceInfo.main_storage))
                    //{
                    //    SetStorage(disk_main, psdState.DeviceInfo.main_storage, psdState.DeviceInfo.main_storage_total, psdState.DeviceInfo.main_storage_free,
                    //        (psdState.DeviceInfo.main_storage == psdState.DeviceInfo.current_storage ? 1 : 2));
                    //}
                    //else
                    //{
                    //    disk_main.Visible = false;
                    //}

                    //DiskInfo disk_second = palDisk.Controls[0] as DiskInfo;
                    //if (!string.IsNullOrEmpty(psdState.DeviceInfo.second_storage))
                    //{
                    //    SetStorage(disk_second, psdState.DeviceInfo.second_storage, psdState.DeviceInfo.second_storage_total, psdState.DeviceInfo.second_storage_free,
                    //        (psdState.DeviceInfo.second_storage == psdState.DeviceInfo.current_storage ? 1 : 2));
                    //}
                    //else
                    //{
                    //    disk_second.Visible = false;
                    //}
                }
            }
            catch (Exception ex)
            { }
        }

        //private void SetStorage(DiskInfo disk, string diskDriver, double total, double free, int storageType)
        //{
        //    try
        //    {
        //        //DiskInfo diskInfo = new DiskInfo();
        //        //palDisk.Controls.Add(diskInfo);
        //        //diskInfo.Dock = DockStyle.Left;
        //        //diskInfo.BringToFront();
        //        disk.DiskDriver = diskDriver;
        //        disk.VolumeName = "";
        //        disk.SumSpace = total;
        //        disk.FreeSpace = free;
        //        disk.StorageType = storageType;
        //        disk.Visible = true;
        //    }
        //    catch { }
        //}

        private void SetStorage(DiskInfo disk, dl_download_device_storage storage)
        {
            try
            {
                disk.Name = storage.id;
                disk.DiskDriver = storage.storage_driver;
                disk.VolumeName = storage.storage_name;
                disk.SumSpace = storage.storage_total_size;
                disk.FreeSpace = storage.storage_free_size;
                disk.StorageType = storage.storage_type;
                disk.Group = storage.storage_group;
            }
            catch { }
        }

        public void GetDownloadServer()
        {
            var result_server = HttpHelper.Get<HResult<List<dl_server_config>>>(AppSettings.Setting.API.GetDownloadServerConfig);

            if (result_server.success)
            {
                ManagerHelper.DownloadServerList = result_server.data;
            }
        }

        private void pbSetDeviceName_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("deviceId", this.psdState.DeviceId);
            data.Add("deviceName", this.txtDeviceName.Text.Trim());
            var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.SetDeviceName, data);

            if (result.success)
            {
                lblDeviceName.Text = txtDeviceName.Text;
                palDeviceName.Visible = false;

                ManagerHelper.SetPSDName(this.psdState.DeviceId, this.lblDeviceName.Text);
            }
            else
            {
                MessageBox.Show(result.message);
            }
        }

        private void pbSetProcess_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("deviceId", this.psdState.DeviceId);
            data.Add("process", this.txtProcess.Value.ToString());
            var result = HttpHelper.Post<HResult>(AppSettings.Setting.API.SetMaxDownloadProcess, data);

            if (result.success)
            {
                palProcess.Visible = false;

                ManagerHelper.SetPSDMaxDownloadProcess(this.psdState.DeviceId, Convert.ToInt32(this.txtProcess.Value));
            }
            else
            {
                MessageBox.Show(result.message);
            }
        }

        private void picEditDeviceName_Click(object sender, EventArgs e)
        {
            txtDeviceName.Text = lblDeviceName.Text;
            palDeviceName.Visible = !palDeviceName.Visible;
        }

        private void picEditProcess_Click(object sender, EventArgs e)
        {
            palProcess.Visible = !palProcess.Visible;
        }

        private void picEditReboot_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定重启设备【" + this.lblDeviceName.Text + "】？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ManagerHelper.RebootPSD(this.psdState.DeviceId);
            }
        }

        private void picEditShutdown_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定关闭设备【" + this.lblDeviceName.Text + "】？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ManagerHelper.ShutdownPSD(this.psdState.DeviceId);
            }
        }

        private void picEditReDL_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定重启【" + this.lblDeviceName.Text + "】的下载服务？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ManagerHelper.RestartPSDDownloadService(this.psdState.DeviceId);
            }
        }
    }
}
